import SDKAd from "./SDKAd";
import { SDKState } from "../SDKDefine";
/**
 * 积木广告
 * https://q.qq.com/wiki/develop/game/API/ad/qq.createBlockAd.html
 */
export default class SDKBlockAd extends SDKAd {


    open(param: { index: number, count: number, dir: string, x: number, y: number }) {
        console.log('QQBlockAd  open state ', this._state)
        if (this._state == SDKState.SUCCESS) {
            this.show()
        } else {
            this.load()
        }
    }
    private onResize(size: any) {
        console.log('QQBlockAd onResize  ', size)
        let winSize = this.sdk.getSystemInfoSync();
        console.log(size.width, size.height);
        if (size.width != 0 && size.height != 0) {
            this.instance.style.top = (winSize.windowHeight - size.height) / 2;
            // this.instance.style.left = (winSize.windowWidth - size.width) / 2;
            this.instance.style.left = winSize.windowWidth - size.width;
        }
    }

    private onError(err: any) {
        console.log('QQBlockAd onError  ', err)
        // this.setState(SDKState.loadFail)
    }

    private onLoad() {
        console.log('QQBlockAd onLoad  ')
        this.setState(SDKState.SUCCESS)
    }

    protected getStyle() {
        let bannerHeight = 120;
        let bannerWidth = 120;
        let winSize = this.sdk.getSystemInfoSync();
        let left = (winSize.windowWidth - bannerWidth)
        let top = (winSize.windowHeight - bannerHeight) / 2;
        return {
            left: left,
            top: top
        }
    }

    load() {
        this.destroy()
        if (!this.instance) {
            this.instance = this.sdk.createBlockAd({
                adUnitId: this._adUnitId,
                style: this.getStyle(),
                size: 1,
                // orientation: 'landscape'
                orientation: 'vertical'
            })
            if (this.instance) {
                this.instance.onResize(this.bind(this.onResize))
                this.instance.onLoad(this.bind(this.onLoad))
                this.instance.onError(this.bind(this.onError))
            }

        }
    }

    protected show() {
        if (this.instance) {
            this.instance.show().then(() => {
                this.setState(SDKState.OPEN)
                console.log(' 积木广告 show ok')
            }).catch(() => {
                // this.channel.showToast('播放失败')
            })
        }
    }

    close() {
        console.log(' 积木广告 close  ok ', this._state)
        if (this._state == SDKState.OPEN) {
            this.hide()
        }
    }

    protected hide() {
        this.setState(SDKState.CLOSE)
        if (this.instance) {
            console.log(' blckad hide ')
            this.instance.hide()
        }
    }

    protected destroy() {
        if (this.instance) {
            this.instance.hide()
            this.instance.offResize(this.unbind(this.onResize))
            this.instance.offLoad(this.unbind(this.onLoad))
            this.instance.offError(this.unbind(this.onError))
            this.instance = null;
        }
    }
}
